package cs422.group1.projectsquirrel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;


public class ExtraSurvey6 extends Activity
{
	//Instance variables.
	private Button nextButton;
	private Spinner dogSpinner;
	private Spinner catSpinner;
	private Spinner coyotesSpinner;
	private Spinner hawksSpinner;
	private Spinner poisonedGrainSpinner;
	
	private JavaSucks dogValue, catValue, coyotesValue, hawksValue, poisonedValue;
	
	private String valueOfSpinner;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.extrasurveypage6);
		dogSpinner = (Spinner)findViewById(R.id.dogSpinner);
		catSpinner = (Spinner)findViewById(R.id.catSpinner);
		coyotesSpinner = (Spinner)findViewById(R.id.coyotesSpinner);
		hawksSpinner = (Spinner)findViewById(R.id.hawksSpinner);
		poisonedGrainSpinner = (Spinner)findViewById(R.id.poisonedGrainSpinner);
		nextButton = (Button)findViewById(R.id.nextButton6);
		
		dogValue = new JavaSucks("");
		catValue = new JavaSucks("");
		coyotesValue = new JavaSucks("");
		hawksValue = new JavaSucks("");
		poisonedValue = new JavaSucks("");
		
		
		//Listeners!!!
		//Dog Spinner/Adapter
        ArrayAdapter<CharSequence> dogAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverHigh, R.layout.spinner_style);
        dogAdapter.setDropDownViewResource(R.layout.combo_style);
        dogSpinner.setAdapter(dogAdapter);
        dogSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(dogValue));
		
        ArrayAdapter<CharSequence> catAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverHigh, R.layout.spinner_style);
        catAdapter.setDropDownViewResource(R.layout.combo_style);
        catSpinner.setAdapter(catAdapter);
        catSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(catValue));
        
        ArrayAdapter<CharSequence> coyoteAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverHigh, R.layout.spinner_style);
        coyoteAdapter.setDropDownViewResource(R.layout.combo_style);
        coyotesSpinner.setAdapter(coyoteAdapter);
        coyotesSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(coyotesValue));
		
        ArrayAdapter<CharSequence> hawkAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverHigh, R.layout.spinner_style);
        hawkAdapter.setDropDownViewResource(R.layout.combo_style);
        hawksSpinner.setAdapter(hawkAdapter);
        hawksSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(hawksValue));
		
        ArrayAdapter<CharSequence> poisonedGrainAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverHigh, R.layout.spinner_style);
        poisonedGrainAdapter.setDropDownViewResource(R.layout.combo_style);
        poisonedGrainSpinner.setAdapter(poisonedGrainAdapter);
        poisonedGrainSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(poisonedValue));
        
        if (ObservationDataManager.isEdit())
        {
        	dogSpinner.setSelection(dogAdapter.getPosition(ObservationDataManager.getData().site_dogs));
        	catSpinner.setSelection(catAdapter.getPosition(ObservationDataManager.getData().site_cats));
        	coyotesSpinner.setSelection(coyoteAdapter.getPosition(ObservationDataManager.getData().site_coyotes));
        	hawksSpinner.setSelection(hawkAdapter.getPosition(ObservationDataManager.getData().site_hawks));
        	poisonedGrainSpinner.setSelection(poisonedGrainAdapter.getPosition(ObservationDataManager.getData().site_grain));
        	
        }
        
        
        //Attaching a listener for callback on the next button.
        nextButton.setOnClickListener(new nextQuestionListener()); 
	}
	//Callback function for the drop down box on the current page.
	public class myOnItemSelectedListener implements OnItemSelectedListener
	{
		JavaSucks val;
		public myOnItemSelectedListener(JavaSucks _val)
		{
			val = _val;
		}
		public void onItemSelected(AdapterView<?> parent,View view,int pos,long id)
		{
			  val.setValue(parent.getItemAtPosition(pos).toString());
		}
		public void onNothingSelected(AdapterView<?> parent)
		{
			//Do Nothing!
		}
	}	
	
	
	private void gatherData()
	{
		ObservationData od = ObservationDataManager.getData();
		od.site_dogs = dogValue.getValue();
		od.site_cats = catValue.getValue();
		od.site_coyotes = coyotesValue.getValue();
		od.site_hawks = hawksValue.getValue();
		od.site_grain = poisonedValue.getValue();
		
	}
	
	//Callback function for the NEXT button.
	public class nextQuestionListener implements OnClickListener
	{
		public void onClick(View view)
		{
			gatherData();
			nextPage();
		}
	}
		
	
	//Done with this activity, and moving on to the next one.
	private void nextPage()
	{
		Intent intent = new Intent(this.getBaseContext(),ExtraSurvey7.class);
		startActivity(intent);
	}

	
}
